package net.hn.hnms.biz.common.ihn.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import net.hn.hnms.biz.common.ihn.result.RequestHuaNengTextBean;
import net.hn.hnms.biz.common.ihn.result.ResultTextMessage;
import net.hn.hnms.biz.common.ihn.result.ResultToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Data
@Component
public class HttpRestUtils {

    /**
     * http post
     */
    public static String post(String url, MultiValueMap<String, String> params) throws IOException {
        return httpRestClient(url, HttpMethod.POST, params);
    }

    public static ResultTextMessage sendPOSTMessage(String url, RequestHuaNengTextBean params) throws IOException {
        return httpRestClient(url, HttpMethod.POST, params);
    }

    /**
     * http get
     */
    public static String get(String url, MultiValueMap<String, String> params) throws IOException {
        return httpRestClient(url, HttpMethod.POST, params);
    }

    /**
     * http get
     */
    public static ResultToken getHuaNengToken(String url) throws IOException {
        return httpRestClient(url, HttpMethod.POST);
    }


    @Autowired
    public static RestTemplate client;

    @Autowired
    public void setClient(RestTemplate restTemplate) {
        HttpRestUtils.client = restTemplate;
    }


    /**
     * HttpMethod  post/get
     */
    private static String httpRestClient(String url, HttpMethod method, MultiValueMap<String, String> params) throws IOException {
        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = null;
        try {
            response = client.exchange(url, method, requestEntity, String.class);
            return response.getBody();
        } catch (HttpClientErrorException e) {
            System.out.println("------------- 出现异常 HttpClientErrorException -------------");
            System.out.println(e.getMessage());
            System.out.println(e.getStatusText());
            System.out.println("-------------responseBody-------------");
            System.out.println(e.getResponseBodyAsString());
            e.printStackTrace();
            return "";
        } catch (Exception e) {
            System.out.println("------------- HttpRestUtils.httpRestClient() 出现异常 Exception -------------");
            System.out.println(e.getMessage());
            return "";
        }
    }

    private static ResultToken httpRestClient(String url, HttpMethod method) throws IOException {

        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity requestEntity = new HttpEntity<>(headers);
        //  执行HTTP请求
        ResponseEntity<ResultToken> response = null;
        try {
            response = client.exchange(url, method, requestEntity, ResultToken.class);
            return response.getBody();
        } catch (HttpClientErrorException e) {
            System.out.println("------------- 出现异常 HttpClientErrorException -------------");
            System.out.println(e.getMessage());
            System.out.println(e.getStatusText());
            System.out.println("-------------responseBody-------------");
            System.out.println(e.getResponseBodyAsString());
            e.printStackTrace();
            return new ResultToken();
        } catch (Exception e) {
            System.out.println("------------- HttpRestUtils.httpRestClient() 出现异常 Exception -------------");
            System.out.println(e.getMessage());
            return new ResultToken();
        }
    }

    private static ResultTextMessage httpRestClient(String url, HttpMethod method, RequestHuaNengTextBean params) throws IOException {
        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> requestEntity = new HttpEntity<String>(JSONObject.toJSONString(params), headers);
        //  执行HTTP请求
        ResponseEntity<ResultTextMessage> response = null;
        try {
            response = client.exchange(url, method, requestEntity, ResultTextMessage.class);
            return response.getBody();
        } catch (HttpClientErrorException e) {
            System.out.println("------------- 出现异常 HttpClientErrorException -------------");
            System.out.println(e.getMessage());
            System.out.println(e.getStatusText());
            System.out.println("-------------responseBody-------------");
            System.out.println(e.getResponseBodyAsString());
            e.printStackTrace();
            return new ResultTextMessage();
        } catch (Exception e) {
            System.out.println("------------- HttpRestUtils.httpRestClient() 出现异常 Exception -------------");
            System.out.println(e.getMessage());
            return new ResultTextMessage();
        }

    }
}